﻿#define _CRT_SECURE_NO_WARNINGS 1

// 1.kotori和气球
#include <iostream>

using namespace std;

int n, m;
int main()
{
    cin >> n >> m;

    int ret = n;
    for (int i = 1; i < m; ++i)
    {
        ret *= n - 1;
        if (ret > 109)
            ret %= 109;
    }
    cout << ret << endl;
    return 0;
}

// 2.走迷宫
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 1010;
int dx[4] = { 0, 0, 1, -1 };
int dy[4] = { 1, -1, 0, 0 };
int n, m;
int x1, y1, x2, y2;
char arr[N][N];
int dist[N][N]; // [i, j] 位置是否已经搜索过，以及到达 [i, j] 位置的最短距离
int bfs()
{
	if (arr[x2][y2] == '*') return -1;

	memset(dist, -1, sizeof dist); // 表⽰还没开始搜索
	queue<pair<int, int>> q;
	q.push({ x1, y1 });
	dist[x1][y1] = 0;
	while (q.size())
	{
		auto [a, b] = q.front();
		q.pop();
		for (int i = 0; i < 4; i++)
		{
			int x = a + dx[i], y = b + dy[i];
			if (x >= 1 && x <= n && y >= 1 && y <= m && arr[x][y] == '.' &&
				dist[x][y] == -1)
			{
				q.push({ x, y });
				dist[x][y] = dist[a][b] + 1;
				if (x == x2 && y == y2) return dist[x2][y2];
			}
		}
	}
	return -1;
}
int main()
{
	cin >> n >> m >> x1 >> y1 >> x2 >> y2;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			cin >> arr[i][j];
		}
	}
	cout << bfs() << endl;
	return 0;
}

// 3.主持人调度（二）
class Solution
{
public:
    int minmumNumberOfHost(int n, vector<vector<int> >& startEnd)
    {
        sort(startEnd.begin(), startEnd.end());

        // 创建⼀个⼩根堆
        priority_queue<int, vector<int>, greater<int>> heap;

        // 先把第⼀个区间放进去
        heap.push(startEnd[0][1]);

        // 处理剩下的区间
        for (int i = 1; i < n; i++)
        {
            int a = startEnd[i][0], b = startEnd[i][1];
            if (a >= heap.top()) // 没有重叠
            {
                heap.pop();
                heap.push(b);
            }
            else // 有重叠
            {
                heap.push(b); // 重新安排⼀个⼈
            }
        }
        return heap.size();
    }
};